package com.example.woke1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.woke.data.Data_nearbyperson;
import com.zhongcai.online.R;

import java.util.ArrayList;

public class GaodemapActivity extends Activity implements LocationSource, AMapLocationListener {

	private MapView mapView;
	private AMap aMap;
	private LatLng CHENGDU;
	private ArrayList<Data_nearbyperson> datalist;
	private String user_nicename;

	private OnLocationChangedListener mListener;
	private AMapLocationClient mlocationClient;
	private AMapLocationClientOption mLocationOption;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		getWindow ().clearFlags (WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		setContentView (R.layout.activity_gaodemap);

		Intent intent = getIntent ();
		double latitude = intent.getDoubleExtra ("latitude", 0);
		double longitude = intent.getDoubleExtra ("longitude", 0);
		datalist = (ArrayList<Data_nearbyperson>) intent.getSerializableExtra ("data");
		user_nicename = intent.getStringExtra ("user_nicename");

		CHENGDU = new LatLng (latitude, longitude);
		mapView = (MapView) findViewById (R.id.map);
		mapView.onCreate (savedInstanceState); // �˷���������д
		findViewById (R.id.gaode_image_back).setOnClickListener (new OnClickListener () {
			@Override
			public void onClick(View v) {
				finish ();
			}
		});
		init ();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy ();
		//在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
		mapView.onDestroy ();
	}

	@Override
	protected void onResume() {
		super.onResume ();
		//在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
		mapView.onResume ();
	}

	@Override
	protected void onPause() {
		super.onPause ();
		//在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
		mapView.onPause ();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState (outState);
		//在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
		mapView.onSaveInstanceState (outState);
	}


	/**
	 * ��ʼ��AMap����
	 */
	private void init() {
		if (aMap == null) {
			aMap = mapView.getMap ();
			setUpMap ();
		}
	}

	private void setUpMap() {
//		MyLocationStyle myLocationStyle = new MyLocationStyle ();
//		myLocationStyle.myLocationIcon (BitmapDescriptorFactory
//				.fromResource (R.drawable.location_marker));// 设置小蓝点的图标
//
//		aMap.setMyLocationStyle (myLocationStyle);


		aMap.animateCamera (CameraUpdateFactory.zoomTo (15f));
		aMap.setLocationSource (this);// 设置定位监听
		aMap.getUiSettings ().setMyLocationButtonEnabled (true);// 设置默认定位按钮是否显示
		aMap.setMyLocationEnabled (true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
//		addMarkersToMap ();//
	}

	/**
	 * �ڵ�ͼ�����marker
	 */
//	private void addMarkersToMap() {
//
//
//		for (int i = 0; i < datalist.size (); i++) {
//			Data_nearbyperson person = datalist.get (i);
//			LatLng latLng = new LatLng (person.getLng (), person.getLat ());
//			MarkerOptions markerOptions = new MarkerOptions ();
//			markerOptions.anchor (0.5f, 0.5f)
//					.position (latLng).title (person.getUser_nicename ())
//					.snippet ("").icons (null)
//					.icon (BitmapDescriptorFactory.fromResource (R.drawable.location_other))
//					.draggable (true).period (50);
//			Marker marker = aMap.addMarker (markerOptions);
//			marker.showInfoWindow ();
//		}
//		MarkerOptions markerOption = new MarkerOptions ();
//		markerOption.anchor (0.5f, 0.5f)
//				.position (CHENGDU)
//				.title ("")
//				.snippet (user_nicename)
//				.icon (BitmapDescriptorFactory.fromResource (R.drawable.location))
//				.draggable (true)
//				.period (50);
//		Marker marker = aMap.addMarker (markerOption);
//		marker.showInfoWindow ();
//
//
//	}

	/**
	 * 激活定位
	 */
	@Override
	public void activate(OnLocationChangedListener listener) {
		mListener = listener;
		if (mlocationClient == null) {
			mlocationClient = new AMapLocationClient (this);
			mLocationOption = new AMapLocationClientOption ();
			//设置定位监听
			mlocationClient.setLocationListener (this);
			//设置为高精度定位模式
			mLocationOption.setLocationMode (AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
			//设置定位参数
			mlocationClient.setLocationOption (mLocationOption);
			// 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
			// 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
			// 在定位结束后，在合适的生命周期调用onDestroy()方法
			// 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
			mlocationClient.startLocation ();
		}
	}

	/**
	 * 停止定位
	 */
	@Override
	public void deactivate() {
		mListener = null;
		if (mlocationClient != null) {
			mlocationClient.stopLocation ();
			mlocationClient.onDestroy ();
		}
		mlocationClient = null;
	}


	/**
	 * 定位成功后回调函数
	 */
	@Override
	public void onLocationChanged(AMapLocation amapLocation) {
		if (mListener != null && amapLocation != null) {
			if (amapLocation != null
					&& amapLocation.getErrorCode () == 0) {
				mListener.onLocationChanged (amapLocation);// 显示系统小蓝点
			} else {
				String errText = "定位失败," + amapLocation.getErrorCode () + ": " + amapLocation.getErrorInfo ();
				Log.e ("AmapErr", errText);
			}
		}
	}
}
